package com.qhy.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import dao.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qiuhongyu
 * @date 2020/11/27 14:11
 */
@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return "==========testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "==========testB";
    }

    @GetMapping("/testD")
    public String testD() {
//        try {
//            //次数休眠1秒
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int i = 10/0;

        return "==========testD";
    }

    /**
     * 兜底的降级的方法
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "qhytestHotkey", blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2) {

        int i = 10/0;
        return "==========testHotKey";
        //
    }


    public String deal_testHotKey(String k1, String k2, BlockException blockException) {
        return "********deal_testHotKey -_- *********";
    }

    @GetMapping("/qhyRateLimit/qhyByUrl")
    public CommonResult<?> testPersistence () {
        return new CommonResult<Object>(200,"按url限流测试ok");

    }

}
